package com.deno.taskdemo.net;

import java.util.HashMap;
import java.util.Observer;

import android.content.Context;
import android.os.Handler;

public abstract class Task implements Runnable, Observer {
	String debug = getClass().getSimpleName();
	/** 请求参数 */
	protected Object parameter;
	/** 任务开始监听 */
	protected OnStartListen onStartListen;
	/** 任务进度监听 */
	protected OnProgressListen onProgressListen;
	/** 任务完成监听 */
	protected OnFinishListen onFinishListen;
	/** 任务在队列中完成 监听 */
	protected OnSystemStartListen onSystemStartListen;
	/** 任务在队列中开始 监听 */
	protected OnSystemFinishListen onSystemFinishListen;
	/** 用于任务完成后发送消息 */
	protected Handler handler;
	/** 结果 */
	protected Object result;
	/** 任务编号标示 */
	protected int taskID;
	/** 任务名字标示 */
	/** 设置此任务名是否为单例，单例模式下，如果相同名字的任务未执行完，则无法添加新任务 */
	protected String singletonName;

	/** 保存一个对象 */
	protected Object tag;
	/** 获得当前自身线程的引用 在threadRun方法 */
	protected Thread thread;
	/** 重连次数 */
	protected int tryAgainCount = 1;
	/** 重连间隔 */
	protected int tryAgainTime = 1000;
	/** 视图绑定 */
	protected TaskViewHolder taskViewHolder;

	/** 任务观察者 */
	protected TaskObservable taskObservable;

	protected Context context;
	// 名字映射
	private static HashMap<String, Task> nameTasks = new HashMap<String, Task>();

	public static HashMap<String, Task> getNameTask() {
		return nameTasks;
	}

	protected Task() {
	}

	protected Task(TaskViewHolder taskViewHolder) {
		super();
		this.taskViewHolder = taskViewHolder;
		this.handler = taskViewHolder.getActivityHandler();
		this.taskViewHolder.getTaskObservable().addObserver(this);
		this.context = taskViewHolder.getContext();
		this.taskObservable = this.taskViewHolder.getTaskObservable();
	}

	protected Task(TaskObservable taskObservable) {
		super();
		taskObservable.addObserver(this);
	}

	public TaskViewHolder getTaskViewHolder() {
		return taskViewHolder;
	}

	public void setTaskViewHolder(TaskViewHolder taskViewHolder) {
		this.taskViewHolder = taskViewHolder;
	}
	
	/** 状态 */
	int status = UNTREATED;

	final static int ERROR = -1;
	final static int UNTREATED = 0;
	final static int FINSH = 1;
	final static int TREATEDING = 2;

	// 不处理
	final static int WITHOUT = 3;
	
	
	

	public void setSingletonName(String singletonName) {
		this.singletonName = singletonName;
	}

	public String getSingletonName() {
		return singletonName;
	}

	public interface OnStartListen {
		public void onStart(int taskID);
	}

	public interface OnProgressListen {
		public void onProgress(int taskID, int progress);
	}

	public interface OnFinishListen {
		public Boolean OnFinish(int taskID, Object data);
	}

	public interface OnSystemStartListen {
		public void onSystemStart(int taskID);
	}

	public interface OnSystemFinishListen {
		public Boolean OnSystemFinish(int taskID, Object data, Task t);
	}
}
